package edu.ccut.saturn.studio.logic.model.saturn;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.graphics.Image;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.freemaker.FreemarkerUtils;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;
import edu.ccut.saturn.studio.dialog.iterator.SaturnEndIteratorDialog;
import edu.ccut.saturn.studio.export.createhtml.Parameters;
import edu.ccut.saturn.studio.logic.figure.SaturnFigure;
import edu.ccut.saturn.studio.logic.model.Shape;

public class SaturnEndIterationNode extends Shape {

	public static final String KEY_NODE_ENDITERATION = "end-iteration";

	public static final String KEY_PROPERTY_ENDITERATION_NODE_NAME = "_property_name";

	public static final String KEY_PROPERTY_ENDITERATION_NODE_DESCRITPION = "_property_description";

	public static final String KEY_PROPERTY_ENDITERATION_NODE_START_ID = "_property_start-id";

	public static final String SHOW_ENDITERATION_NODE_NAME = "name";

	public static final String SHOW_ENDITERATION_NODE_DESCRITPION = "description";

	public static final String SHOW_ENDITERATION_NODE_START_ID = "start-id";

	private static final long serialVersionUID = 1L;
	
	private static int i = 1;

	private String startid = "";
	
//	private String startname= "";

	private Image image = Activator.getImage(Images.FOR_END_ICON);

	public SaturnEndIterationNode() {
		super.setName(Messages.getString("SaturnEndIterationNode.EndLoop") + (i++)); //$NON-NLS-1$
	}

	@Override
	public Image getIcon() {
		return image;
	}

	@Override
	public IFigure getFigure() {
		SaturnFigure saturnFigure = new SaturnFigure();
		saturnFigure.setIcon(image);
		saturnFigure.setName(super.getName());

		return saturnFigure;
	}

	public String getStartid() {

		return startid;
	}

	public void setStartid(String startid) {
		if (!this.startid.equals(startid)) {
			this.startid = startid;
			firePropertyChange(PROPERTERY_CHANGS, null, startid);
		}

	}
	

	@Override
	public Node doRefresh() {
		Node root = super.getRoot();

		Node endIterationNode = SaturnW3CUtil.createChildNode(root,
				KEY_NODE_ENDITERATION, null);

		SaturnW3CUtil.createChildNode(endIterationNode,
				SHOW_ENDITERATION_NODE_NAME, super.getName());

		SaturnW3CUtil.createChildNode(endIterationNode,
				SHOW_ENDITERATION_NODE_START_ID, this.startid);

		return endIterationNode;
	}

	public Dialog getDialog() {
//		return new SaturnNormalDialog(super.getShell(), this);
		return new SaturnEndIteratorDialog(super.getShell(), this);
	}

	@Override
	public Shape cloneShape(Point mousePoint, Shape shape) {
		SaturnEndIterationNode endStartIterationNode = new SaturnEndIterationNode();
		SaturnEndIterationNode saturn = (SaturnEndIterationNode) shape;
		endStartIterationNode.setLocation(mousePoint);
		endStartIterationNode.setName(saturn.getName());
		endStartIterationNode.setDescription(saturn.getDescription());
		endStartIterationNode.setStartid(saturn.getStartid());
		endStartIterationNode.setId(saturn.getId());
		endStartIterationNode.setNextId(saturn.getNextId());
		return endStartIterationNode;
	}

	@Override
	protected void setImage(Image image) {
		this.image = image;
	}

	public String[] getStartIteratorIds() {
		List<String> ids = new ArrayList<String>();

		List<Shape> shapes = this.getAllShapes();

		if (shapes != null) {
			for (Shape shape : shapes) {
				if (shape instanceof SaturnStartIterationNode) {
					ids.add(shape.getName() + "[" + shape.getId() + "]");
				}
			}
		}

		return ids.toArray(new String[ids.size()]);
	}

	@Override
	public Parameters createNodeHtml(String filePath, String[] tempnames) {
		
		Map<String, String> infos = new HashMap<String, String>();
		String name = getName();
		Parameters para = new Parameters();
		
		infos.put("name", getName());
	 	infos.put("id", getId());
	 	infos.put("description", getDescription());
		
		para.setInfourl( name + "-end-iteration.html");
		para.setInfoname( name );
		para.setDescription(getDescription());
		para.setNodeType(Messages.getString("Logic.IteratorEndNode")); //$NON-NLS-1$
		para.setImagename("end-iteration");
		String templatePath = "template/export/logic/enditeration";
		String Path = filePath + File.separator + name + "-end-iteration.html";
		FreemarkerUtils.writeTemplateToFile(templatePath,infos, Path);
		
		return para;
	}
}
